package com.lottery.util;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.lottery.entity.Constants;

public class MailUtil {

	public static void send(String mainTo,String msg) throws Exception{
		Session session  = createSession();
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(Constants.mail));
		message.setRecipient(RecipientType.TO, new InternetAddress(mainTo));
		message.setSubject(Constants.mailSubject);
		message.setContent(msg, "text/html;charset=utf-8");
		Transport.send(message);
	}
	
	public static String appendContent(String name ,String account,String password,String url){
		String a = "<td style=\"border-left: 1px solid #D1FFD1; padding: 10px 20px; background: none repeat scroll 0% 0% #ffffff; border-right: 1px solid #D1FFD1;\">";
		
		String b = "<p>"+name+" , 你好</p><p>您已经成功注册为彩票社区会员</p><br><p>您的登录帐号是 <strong>";
		
		String c = "</strong>，初始密码是 <strong>";
		
		String d = "</strong></p><p style=\"font-weight:bold\">请点击以下链接激活此帐号：<br><a href=\"";
		
		String e = "\" target=\"_blank\">";
		
		String f = "</a></p><p>";
		return a + b + account + c + password + d + url + e + url + f;
	}
	
	public static Session createSession() {
		Properties prop = new Properties();
		prop.setProperty("mail.host", Constants.mailHost);
		prop.setProperty("mail.smtp.auth", "true");
		
		Authenticator auth = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(Constants.mailUsername, Constants.mailPassword);
			}
		};
		return Session.getInstance(prop, auth);
	}

	public static void main(String[] args) throws Exception {
		send("867178023@qq.com",appendContent("叶阳","yeyang", "qwer@1234", "http://www.baidu.com"));
	}
}
